<template>
    <AppHeader title="单项填空" :banAnimate="true" backgroundColor="#ffffff">
      <template #left>
        <img
          @click="onClickLeft"
          src="@/assets/images/icon-back-black.png"
          width="9"
          height="18"
        />
      </template>
      <template #right>
        <img
          @click="onClickRight"
          src="@/assets/images/listening/copy.png"
          width="18"
          height="18"
        />
      </template>
    </AppHeader>
    <div class="container">
      <div class="top">
        <div class="type">填空</div>
        <div class="num"><span>{{active+1}}</span>/{{datalist.length}}</div>
      </div>
      <div class="swiper-list">
        <div class="list-item" v-for="(item,i) in datalist" :key="i" v-show="i===active">
            <div class="title" v-html="item.stem" id="dsfs"></div>
        </div>
      </div>
      <div class="btns">
        <div class="btn" @click="prevclick">
          <van-button type="default" block round >上一题</van-button>
        </div>
        <div class="btn" @click="nextclick">
          <van-button type="primary" block round color="linear-gradient( 270deg, #29F19C 0%, #02A1F9 100%)">下一题</van-button>
        </div>
      </div>
    </div>
</template>

<script setup>
import AppHeader from "@/components/AppHeader.vue";
import { useRouter,useRoute } from "vue-router";
import { ref,onMounted ,onBeforeUnmount,nextTick} from "vue";
const router = useRouter();
const route = useRoute();
const resourceObj = ref({})
const score = ref(0)
var time = 0
const tiankong1 = ref('')
const onClickLeft = () => {
  router.back();
};
const onClickRight = () => { 
  // router.push('/written/singleTest')
}
const datalist = ref([])
onMounted(()=>{
  resourceObj.value = JSON.parse(sessionStorage.getItem('resource'))
  if(route.query.pId){
    datalist.value = [resourceObj.value.parts.find(item => item.sid == route.query.sid).children.find(l=>l.sid == route.query.pId)]
    return
  }
  datalist.value = resourceObj.value.parts.find(item => item.sid == route.query.sid).children
  // datalist.value.forEach(item=>{
  //   item.answer={
  //       isAnswer:false,
  //       sType:'',
  //       sorceTotal: 1,
  //       sorce: -1
  //   }
  // })
  getObj()
})
var timer = setInterval(async ()=>{
  time ++
},1000)
onBeforeUnmount(()=>{
  clearInterval(timer)
  timer = null
})
const getObj = ()=>{
  nextTick(()=>{
    if(datalist.value.length > 1){
      document.querySelectorAll('#dsfs input')[active.value].value = datalist.value[0].children[active.value]?datalist.value[0].children[active.value][0].answer.sType:''
    }else{
        datalist.value[0].children.forEach((item,index)=>{
          document.querySelectorAll('#dsfs input')[index].setAttribute('value', item.answer.sType);
        })
      }
  })
}
const active = ref(0);
const inx = ref(null);
const prevclick = () => { 
  //如果是已经选择了，进行回填
  if(active.value > 0){
    active.value --
    getObj()
  }else{
    var num = resourceObj.value.parts.findIndex(item => item.sid == route.query.sid)
    if(num < resourceObj.value.parts.length){
      router.push({
        path: "/writtenTest/single",
        query: { sid: resourceObj.value.parts[num - 1].sid },
      });
    }
  }
}
const nextclick = () => {
  if(active.value + 1 < datalist.value.length){
    active.value ++
    getObj()
  }else{
    var num = resourceObj.value.parts.findIndex(item => item.sid == route.query.sid)
    if(num + 1 < resourceObj.value.parts.length){
      router.push({
        path: "/writtenTest/single",
        query: { sid: resourceObj.value.parts[num + 1].sid },
      });
    }else{
      router.push('/writtenTest/singleSubmit')
      sessionStorage.setItem('time',time)
      clearInterval(timer)
    }
  }
  debugger
  if(datalist.value.length > 1){
    datalist.value[0].children[active.value-1] = 
      {
        answer:{
          sorce:document.querySelectorAll('#dsfs input')[active.value-1].value=== JSON.parse(datalist.value[0].ref_info.answers[0])[0] ? 1 : 0,
          isAnswer:document.querySelectorAll('#dsfs input')[active.value-1].value=== JSON.parse(datalist.value[0].ref_info.answers[0])[0],
          sType:document.querySelectorAll('#dsfs input')[active.value-1].value,
        }
      }
    datalist.value[0].children[active.value-1]
  }else{
    document.querySelectorAll('#dsfs input').forEach((item,index)=>{
      datalist.value[0].children[index].answer={
          sorce:item.value=== JSON.parse(datalist.value[0].ref_info.answers[index])[0] ? 1 : 0,
          isAnswer:item.value=== JSON.parse(datalist.value[0].ref_info.answers[index])[0],
          sType:item.value,
        }
    })
    console.log(datalist.value)
  }
  
  if(route.query.pId){
    var k = resourceObj.value.parts.find(item => item.sid == route.query.sid).children
    var e = k.findIndex(l=>l.sid == route.query.pId)
    k[e]=  JSON.parse(JSON.stringify(datalist.value[0]))
    sessionStorage.setItem('resource',JSON.stringify(resourceObj.value))
    return
  }
  resourceObj.value.parts.find(item => item.sid == route.query.sid).children =  datalist.value
  sessionStorage.setItem('resource',JSON.stringify(resourceObj.value))
  
  
}

</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  background-color: #ffffff;
  padding-top: 44px;
  padding-bottom: 100px;
  overflow-y: auto;
 .top{
  
  padding: 15px 10px;
  display: flex;
  align-items: center;
  justify-content:space-between ;
  .type{
    font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 16px;
  color: #333333;
  line-height: 22px;
  text-align: left;
  font-style: normal;
  }
  .num{
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 16px;

    line-height: 22px;
    text-align: center;
    font-style: normal;
    span{
      color: #0CB76C;
    }
  }
 }
 .swiper-list{
  .list-item{
    padding: 0 15px;
    .title{
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 18px;
      color: #333333;
      line-height: 25px;
      text-align: left;
      font-style: normal;
    }
    .option{
      margin: 15px 0;
      .checked{
        background: #0CB76C;
        color: #ffffff;
      }
      .unchecked{
        display:inline-block;
        width: 30px;
        height: 30px;
        border: 1px solid #0CB76C;
        border-radius: 50%;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 16px;
        color: #0CB76C;
        text-align: left;
        font-style: normal;
        text-align: center;
        line-height: 28px;
      }
      .uActive{
        color: #fff;
        background: #0CB76C;
      }
      .option-item{
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        font-size: 18px;
        color: #8E8E8E;
        line-height: 25px;
        text-align: left;
        font-style: normal;
        margin-left: 15px;
      }
    }
  }
 }
 .btns{
  display: flex;
    padding: 30px 10px;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    .btn{
      flex:1;
      &:first-child{
        margin-right: 15px;
      }
    }
 }
}
</style>
